//
//  VipPromptTableViewCell.swift
//  LeTu
//
//  Created by g1 on 2023/12/31.
//

import UIKit

struct VipPromptItem {
    var title: String
    var content: String
}

class VipPromptTableViewCell: BaseTableViewCell, UITableViewDelegate, UITableViewDataSource {

    private var sourceArr = [VipPromptItem]()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        sourceArr = VipPromptTableViewCell.getPromptSource()
        
        configUI()
    }
    private static func getPromptSource() -> [VipPromptItem] {
        return [VipPromptItem(title: "1、购买了\"乐途旅游讲解\"会员后，能退款吗？".localized, content: "因为\"乐途旅游讲解\"的会员服务为知识产品，不支持退款服务".localized),
                VipPromptItem(title: "2、会员权益可以几个人同时使用吗？".localized, content: "不可以，会员权益为个人专享权益，如涉及手机号更换等情况，请联系客服为您做解绑操作，解绑后原号码将无法登陆使用，且一年内只能解绑一次。".localized),
                VipPromptItem(title: "3、购买了会员后多久会员权益生效?".localized, content: "支付完成后，会员将在1分钟内生效。如果确认支付成功但会员权益没有生效，建议退出账号重新登录查看权益生效情况。如果权益仍未生效，请联系客服处理。".localized)]
    }
    static func getCellHeight() -> CGFloat {
        let items = getPromptSource()
        var promptHeight = 0.0
        
        for item in items {
            promptHeight += VipPromptItemTableViewCell.getViewHeight(item: item)
        }
        if items.count > 1 {
            promptHeight += CGFloat(items.count-1)*30.0
        }
        
        return 20+25 + (promptHeight > 0 ? 14+30:0) + promptHeight
    }
    
    // MARK: - UITableViewDelegate, UITableViewDataSource
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 30
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return VipPromptItemTableViewCell.getViewHeight(item: sourceArr[indexPath.item])
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sourceArr.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(VipPromptItemTableViewCell.self, indexPath: indexPath)
        cell.setInfo(item: sourceArr[indexPath.item])
        return cell
    }
    
    // MARK: - configUI
    func configUI() {
        
        backgroundColor = .app_white
        contentView.addSubview(titleLabel)
        contentView.addSubview(promptTableView)
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(20)
            make.height.equalTo(25)
        }
        promptTableView.snp.makeConstraints { make in
            make.left.right.equalTo(titleLabel)
            make.bottom.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(14)
        }
    
        promptTableView.delegate = self
        promptTableView.dataSource = self
        promptTableView.register(cellClass: VipPromptItemTableViewCell.self)
        
    }
    private lazy var titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.font = kSetPingFangMedium(18)
        titleLabel.text = "常见问题".localized
        return titleLabel
    }()
    private lazy var promptTableView: UITableView = {
        let promptTableView = UITableView()
        promptTableView.tableFooterView = UIView()
        promptTableView.isScrollEnabled = false
        return promptTableView
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

class VipPromptItemTableViewCell: BaseTableViewCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        configUI()
    }
    static func getViewHeight(item: VipPromptItem) -> CGFloat {
        
        let titleAttri: ASAttributedString = "\(item.title.localized, .kern(1))"
        let titleHeight = titleAttri.value.heightWithSizeThatFits(constrainedToWidth: screenWidth-16*2, font: kSetPingFangRegular(14))+2
        
        let promptAttri: ASAttributedString = "\(item.content.localized, .kern(1))"
        let promptHeight = promptAttri.value.heightWithSizeThatFits(constrainedToWidth: screenWidth-16*2, font: kSetPingFangRegular(12))+2
        return titleHeight + 4 + promptHeight + 16
    }
    func setInfo(item: VipPromptItem) {
        
        titleLabel.attributed.text = "\(item.title.localized, .kern(1))"
        let titleHeight = titleLabel.sizeThatFits(CGSize(width: screenWidth-16*2, height: .greatestFiniteMagnitude)).height+2
        titleLabel.snp.updateConstraints { make in
            make.height.equalTo(titleHeight)
        }
        
        contentLabel.attributed.text = "\(item.content.localized, .kern(1))"
        let promptHeight = contentLabel.sizeThatFits(CGSize(width: screenWidth-16*2, height: .greatestFiniteMagnitude)).height+2
        contentLabel.snp.updateConstraints { make in
            make.height.equalTo(promptHeight)
        }
        
    }
    
    // MARK: - configUI
    func configUI() {
        
        contentView.addSubview(titleLabel)
        contentView.addSubview(contentLabel)
        
        
        titleLabel.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(0)
        }
        contentLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(0)
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
        }
        
    }
    private lazy var titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.font = kSetPingFangRegular(14)
        titleLabel.numberOfLines = 0
        return titleLabel
    }()
    private lazy var contentLabel: UILabel = {
        let contentLabel = UILabel()
        contentLabel.font = kSetPingFangRegular(12)
        contentLabel.textColor = UIColor.hex(hexString: "#868D9A")
        contentLabel.numberOfLines = 0
        return contentLabel
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

